#!/usr/bin/env python
# coding: utf-8

# # API 文档介绍
# * 本周学习主要任务
# * 百度AI开放平台
# * 通用物体和场景识别
# * 1。鉴权认证机制（获取access_token)

# In[8]:


# encoding:utf-8
import requests 

# client_id 为官网获取的AK， client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?'

# 准备URL参数
payload = {
    'grant_type':'client_credentials',
    'client_id':'',
    'client_secret':'tpG69acq8hmY9D5hRlCaKka7QeoUVU2c'
}
response = requests.get(host)
if response:
    print(response.json())


# In[9]:


response.json()


# In[10]:


# encoding:utf-8
import requests 

# client_id 为官网获取的AK， client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=ObMC2DqclMFSnu7xjW0d3Sl4&client_secret=tpG69acq8hmY9D5hRlCaKka7QeoUVU2c'
response = requests.get(host)
if response:
    print(response.json())


# In[ ]:





# In[16]:


# encoding:utf-8

import requests
import base64

'''
通用物体和场景识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
# 二进制方式打开图片文件

f = open('Liu_01.jpeg', 'rb')
img = base64.b64encode(f.read())

params = {"image":img}
access_token = '24.dd9bc181ebf029f20b6bd41258a9698d.2592000.1651665734.282335-25904811'
request_url = request_url + "?access_token=" + access_token

# payload = {
#     'access_token':access_token,
#     'image':img,
#     'baike_num':5
# }

headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())


# In[4]:


# encoding:utf-8

import requests
import base64

'''
通用物体和场景识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
# 二进制方式打开图片文件
# 1.图片文件准备
f = open('niurou.jpg', 'rb')
img = base64.b64encode(f.read())

# 2. 酬载准备
payload={
    'access_token':simin_AT,
    'image':img,
    'baike_num':5
}

headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# # 菜品识别

# In[ ]:


# encoding:utf-8

import requests
import base64

'''
通用物体和场景识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())

params = {"image":img}
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())


# In[6]:


# encoding:utf-8

import requests
import base64

'''
菜品识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/dish"
# 二进制方式打开图片文件
f = open('niurou.jpg', 'rb')
img = base64.b64encode(f.read())


access_token = '24.dd9bc181ebf029f20b6bd41258a9698d.2592000.1651665734.282335-25904811'
payload={
    'access_token':'24.dd9bc181ebf029f20b6bd41258a9698d.2592000.1651665734.282335-25904811',
    'image':img,
    'baike_num':5,
    "top_num":5
}
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# In[ ]:




